package com.smartsofa.main;

import java.io.IOException;
import java.util.ArrayList;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.smartsofa.R;
import com.smartsofa.util.DayLineView;
import com.smartsofa.util.MyUtils;

public class DayHistoryDisplay extends Activity {
	private String dateString;
	private TextView tx_daTextView;
	private Button bt_his_xueya;
    private Button bt_his_xueyang;
    private Button bt_his_xuetang;
    private int styleMode=1;
    private Button bt_his_back;
    private DayLineView lv_history ;
    private JSONObject json;
    private boolean network_timeout=false;
    private Boolean flag=false;
    private int green;
    private int darkgray;
    ArrayList<String> timeData= new ArrayList<String>();
	ArrayList<Float> dataList1= new ArrayList<Float>();
	ArrayList<Float> dataList2= new ArrayList<Float>();
	ArrayList<ArrayList<Float>> dataLists= new ArrayList<ArrayList<Float>>();
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_dayhistory);
		green=getResources().getColor(R.color.reddeep);
	    darkgray=getResources().getColor(R.color.darkgray);
		Intent intent = getIntent();
		dateString = intent.getStringExtra("date");
		styleMode = intent.getIntExtra("styleMode", 1);
		Log.i("stylemode", styleMode+"");
		tx_daTextView = (TextView) findViewById(R.id.tx_date);
		tx_daTextView.setText(dateString);
		lv_history = (DayLineView)findViewById(R.id.line_view);
		bt_his_xueya = (Button) findViewById(R.id.bt_his_xueya);
	    bt_his_xueyang = (Button) findViewById(R.id.bt_his_xueyang);
	    bt_his_xuetang = (Button) findViewById(R.id.bt_his_xuetang);
		
		//back
		bt_his_back=(Button) findViewById(R.id.bt_his_back);
		bt_his_back.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				DayHistoryDisplay.this.finish();
			}
		});
		switch (styleMode){
		case 1:
			bt_his_xueya.setBackgroundColor(green);
			bt_his_xueyang.setBackgroundColor(darkgray);
			bt_his_xuetang.setBackgroundColor(darkgray);
			break;
		case 2:
			
			bt_his_xueya.setBackgroundColor(darkgray);
			bt_his_xueyang.setBackgroundColor(darkgray);
			bt_his_xuetang.setBackgroundColor(green);
			styleMode = 3;
			break;
		case 3:
			
			bt_his_xueya.setBackgroundColor(darkgray);
			bt_his_xueyang.setBackgroundColor(green);
			bt_his_xuetang.setBackgroundColor(darkgray);
			styleMode = 4;
			break;
		}
		Thread thread = new Thread(new GetDataFromServer());
		thread.start();
		
	    
	    final boolean netstate=MyUtils.isNetworkConnected(getApplicationContext());
	    if(!netstate){
			Toast.makeText(DayHistoryDisplay.this,"网络无法连接，请检查你的网络！",Toast.LENGTH_SHORT).show();
		}
	    bt_his_xueya.setOnClickListener( new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if(!netstate){
					Toast.makeText(DayHistoryDisplay.this,"网络无法连接，请检查你的网络！",Toast.LENGTH_SHORT).show();
				}
				bt_his_xueya.setBackgroundColor(green);
				bt_his_xueyang.setBackgroundColor(darkgray);
				bt_his_xuetang.setBackgroundColor(darkgray);
				styleMode=1;		
				 Thread thread =new Thread(new GetDataFromServer());
				 thread.start();	
			}
		});
	    bt_his_xuetang.setOnClickListener( new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if(!netstate){
					Toast.makeText(DayHistoryDisplay.this,"网络无法连接，请检查你的网络！",Toast.LENGTH_SHORT).show();
				}
				bt_his_xueya.setBackgroundColor(darkgray);
				bt_his_xueyang.setBackgroundColor(darkgray);
				bt_his_xuetang.setBackgroundColor(green);
				styleMode=3;
				 Thread thread =new Thread(new GetDataFromServer());
				    thread.start();	
			}
		});
	    bt_his_xueyang.setOnClickListener( new OnClickListener() {
		
	    	@Override
			public void onClick(View v) {
	    		if(!netstate){
					Toast.makeText(DayHistoryDisplay.this,"网络无法连接，请检查你的网络！",Toast.LENGTH_SHORT).show();
				}
	    		bt_his_xueya.setBackgroundColor(darkgray);
				bt_his_xueyang.setBackgroundColor(green);
				bt_his_xuetang.setBackgroundColor(darkgray);
				styleMode=4;	
				 Thread thread =new Thread(new GetDataFromServer());
				    thread.start();	
		}
	});
		
		
		
	}
	class GetDataFromServer implements Runnable{

		@Override
		public void run() {
			String url = MyUtils.getUrl()+"display_dataDay?userid="+MyUtils.getUSERID()+"&datatype="+styleMode+"&date=2015-"+dateString;
			HttpGet request = new HttpGet(url);																
			BasicHttpParams httpParams = new BasicHttpParams();
		    HttpConnectionParams.setConnectionTimeout(httpParams,3000);
	        HttpClient httpClient = new DefaultHttpClient(httpParams);
	        try {
				HttpResponse httpResponse = httpClient.execute(request);
				 if (httpResponse.getStatusLine().getStatusCode() == 200) {
				    	network_timeout=true;  
				        String jsonString = EntityUtils.toString(httpResponse.getEntity());
				        json = new JSONObject(jsonString);
				        flag=(Boolean) json.get("success");       
				        }
			} catch (ClientProtocolException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}	
		
		Message msg = content_handler.obtainMessage();
		if (flag) {				
			msg.what = 1;							
		}
		else if(!flag){
			msg.what = 2;	
		}
		else{
			msg.what = 3;
		}
		content_handler.sendMessage(msg);
		
		
	}
	Handler content_handler = new Handler(){
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case 1:
				
				float dataValue;
				
				switch (styleMode) {
				case 1:					
					styleMode = 2;
					JSONArray jsonarr1;
					dataList1.clear();
					dataList2.clear();
					dataLists.clear();
					timeData.clear();
					
					String time;
					try {
						jsonarr1 = json.getJSONArray("obj");
						for(int i=jsonarr1.length()-1;i>=0;i--){
							time = jsonarr1.getJSONObject(i).getString("uploadTime").substring(11, 19);
							dataValue = (float) jsonarr1.getJSONObject(i).getDouble("dataValue");
							Log.i("SBP", dataValue+"");
							timeData.add(time);
							Log.i("time", time);
							dataList1.add(dataValue);
						}
						
					} catch (JSONException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					Thread thread = new Thread(new GetDataFromServer());
					thread.start();										
					break;
				case 2:
					
					try {
						jsonarr1 = json.getJSONArray("obj");
						for(int i=jsonarr1.length()-1;i>=0;i--){
							dataValue = (float) jsonarr1.getJSONObject(i).getDouble("dataValue");
							dataList2.add(dataValue);
							Log.i("DBP", dataValue+"");
						}
					} catch (JSONException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					dataLists.add(dataList1);
					dataLists.add(dataList2);
					lv_history.setStyleMode(1);
					lv_history.setBottomTextList(timeData);
					lv_history.setDataLists(dataLists);
					break;
				case 3:
					
					dataList1.clear();
					dataList2.clear();
					dataLists.clear();
					timeData.clear();
					try {
						jsonarr1 = json.getJSONArray("obj");
						for(int i=jsonarr1.length()-1;i>=0;i--){
							time = jsonarr1.getJSONObject(i).getString("uploadTime").substring(11, 19);
							dataValue = (float) jsonarr1.getJSONObject(i).getDouble("dataValue");
							timeData.add(time);
							dataList1.add(dataValue);
						}
						
					} catch (JSONException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					dataLists.add(dataList1);
					dataLists.add(dataList2);
					lv_history.setStyleMode(2);
					lv_history.setBottomTextList(timeData);
					lv_history.setDataLists(dataLists);
					break;
				case 4:
				dataList1.clear();
				dataList2.clear();
				dataLists.clear();
				timeData.clear();
				try {
					jsonarr1 = json.getJSONArray("obj");
					for(int i=jsonarr1.length()-1;i>=0;i--){
						time = jsonarr1.getJSONObject(i).getString("uploadTime").substring(11, 19);
						dataValue = (float) jsonarr1.getJSONObject(i).getDouble("dataValue");
						timeData.add(time);
						dataList1.add(dataValue);
					}
					
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				dataLists.add(dataList1);
				dataLists.add(dataList2);
				lv_history.setStyleMode(3);
				lv_history.setBottomTextList(timeData);
				lv_history.setDataLists(dataLists);
				default:
					break;
				}
				break;
			case 2:
				Toast.makeText(getApplicationContext(), "您还未测试，暂时无历史数据，请稍后再试！", Toast.LENGTH_LONG).show();
				break;
			case 3:
				Toast.makeText(getApplicationContext(), "网络连接超时！", Toast.LENGTH_LONG).show();
				break;
			default:
				break;
			}
		};
	};
}

}
